<template>

  <div>
    <div class="filter-container">
      <el-input
        v-model="listQuery.title"
        placeholder="商户订单编号"
        style="width: 150px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="listQuery.importance"
        placeholder="订单状态"
        clearable
        style="width: 150px"
        class="filter-item"
      >
        <el-option
          v-for="item in importanceOptions"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <!--<el-select v-model="listQuery.type"-->
      <!--placeholder="Type"-->
      <!--clearable class="filter-item" style="width: 130px">-->
      <!--<el-option v-for="item in calendarTypeOptions"-->
      <!--:key="item.key"-->
      <!--:label="item.display_name+'('+item.key+')'"-->
      <!--:value="item.key" />-->
      <!--</el-select>-->
      <el-select
        v-model="listQuery.sort"
        style="width: 140px"
        class="filter-item"
      >
        <el-option
          v-for="item in sortOptions"
          :key="item.key"
          :label="item.label"
          :value="item.key"
        />
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <!--<el-button-->
        <!--class="filter-item"-->
        <!--style="margin-left: 10px;"-->
        <!--type="primary"-->
        <!--icon="el-icon-edit"-->
        <!--@click="handleCreate"-->
      <!--&gt;-->
        <!--增加订单-->
      <!--</el-button>-->
      <el-button
        v-waves
        :loading="downloadLoading"
        class="filter-item"
        type="primary"
        icon="el-icon-download"
        @click="handleDownload"
      >
        导出
      </el-button>
      <el-button
        v-waves
        :loading="downloadLoading"
        class="filter-item"
        type="primary"
        icon="el-icon-upload2"
        @click=""
      >
        导入
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <!--<el-table-column label="ID" prop="id" sortable="custom" align="center" width="80">-->
      <!--<template slot-scope="scope">-->
      <!--<span>{{ scope.row.id }}</span>-->
      <!--</template>-->
      <!--</el-table-column>-->

      <el-table-column
        label="商户订单编号"
        prop="merchant_code"
        sortable="custom"
        align="center"
        width="180"
      >
        <!--<template slot-scope="scope">-->
          <!--<span>{{ scope.row.merchant_code }}</span>-->
        <!--</template>-->
      </el-table-column>

      <el-table-column
        label="总价"
        prop="total_prices"
        sortable="custom"
        align="center"
        width="180"
      >
        <!--<template slot-scope="scope">-->
          <!--<span>{{ scope.row.originalPrice===0?scope.row.price:scope.row.originalPrice }}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <!--<el-table-column label="商品图片" prop="picpath"-->
      <!--sortable="custom" align="center" width="180">-->
      <!--<template slot-scope="scope">-->
      <!--<img  width="200" :src="scope.row.picpath" />-->
      <!--&lt;!&ndash;<span>{{ scope.row.picpath }}</span>&ndash;&gt;-->
      <!--</template>-->
      </el-table-column>
      <el-table-column
        label="订单状态"
        prop="status"
        sortable="custom"
        align="center"
        width="180"
      >
        <!--<template slot-scope="scope">-->
          <!--<span>{{ scope.row.buyNumber }}</span>-->
        <!--</template>-->
      </el-table-column>

      <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            修改
          </el-button>
          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDel(row)">
            删除
          </el-button>
          <!--<el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDel(row)">-->
            <!--上架-->
          <!--</el-button>-->
          <!--<el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDel(row)">-->
            <!--下架-->
          <!--</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      small
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
    >

      <el-form ref="dataForm"
      :model="temp"
      label-position="left" label-width="130px" style="width: 400px; margin-left:50px;">
        <template>
          <el-select v-model="value" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      <!--<el-form-item label="地址" prop="productame">-->
      <!--<el-input v-model="temp.productame" />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="商品数量" prop="originalPrice">-->
      <!--<el-input v-model="temp.originalPrice" />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="商品图片" prop="picpath">-->
      <!--<el-input v-model="temp.picpath" />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="库存" prop="inventory">-->
      <!--<el-input v-model="temp.inventory" />-->
      <!--</el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button
          type="primary"
          @click="dialogStatus==='create'?createData():updateData()"
        >
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import $ from 'jquery'
import ajax from '@/utils/baseurl'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'

var p
export default {
  name: 'OrderForm',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      temp: {
        order_time: '',
        time_payment: '',
        a_refund_of_time: '',
        receiving_time: '',
        merchant_code: '',
        we_chat_code: '',
        total_prices: '',
        status: '',
        clear_time: '',
        address: '',
        shop_code: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      downloadLoading: false,
      textMap: {
        update: '修改'
      },
      listQuery: {
        page: 1,
        limit: 10,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      total: 0,
      tableKey: 0,
      listLoading: false,
      list: null,
      importanceOptions: ['待支付', '待取货', '待发货', '待收货', '待评价', '待退款', '已退款', '订单关闭'],
      sortOptions: [{ label: 'ID 升序', key: '+id' },
        { label: 'ID 降序', key: '-id' }]
    }
  },
  created() {
    p = this
    this.getList()
  },
  methods: {
    updateData() {
      this.createData()
      this.getList()
    },
    resetTemp() {
      this.temp = {
        order_time: '',
        time_payment: '',
        a_refund_of_time: '',
        receiving_time: '',
        merchant_code: '',
        we_chat_code: '',
        total_prices: '',
        status: '',
        clear_time: '',
        address: '',
        shop_code: ''
      }
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm']//.clearValidate()// 清除验证
      })
    },
    handleDel(row) {
      var p = this
      this.$confirm('确认删除吗? 是否继续', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ajax('orderForm/orderFormDel', { 'id': row.id }, function(data) {
          if (data) {
            p.getList()
            p.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    sortChange() {

    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    createData() {
      // this.resetTemp();
      var p = this
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          ajax(p.dialogStatus == 'create' ? 'orderForm/orderFormAdd' : 'orderForm/orderFormUpdate', this.temp, function(data) {
            if (data.id) {
              p.dialogFormVisible = false
              p.getList()
              p.$notify({
                title: '添加',
                message: p.dialogStatus === 'create' ? '添加成功' : '修改成功',
                type: 'success',
                duration: 2000
              })
            }
          })
        }
      })
    },
    handleCreate() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleDownload() {

    },
    getList() {
      const p = this
      this.listLoading = true
      console.log(this.listQuery)
      ajax('orderForm/findAll', this.listQuery, function(data) {
        data = JSON.parse(data)
        console.log(data)
        p.list = data.page.list
        p.total = data.page.totalCount
         p.listLoading = false
      })
    }
  }
}
</script>

